package com.ruoyi.ldzlsb.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.ldzlsb.domain.SbInspectionItem;
import com.ruoyi.ldzlsb.service.ISbInspectionItemService;

/**
 * 巡检项Controller
 * 
 * @author ruoyi
 * @date 2023-03-15
 */
@RestController
@RequestMapping("/inspection/item")
public class SbInspectionItemController extends BaseController
{
    @Autowired
    private ISbInspectionItemService sbInspectionItemService;

    /**
     * 查询巡检项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbInspectionItem sbInspectionItem)
    {
        startPage();
        List<SbInspectionItem> list = sbInspectionItemService.selectSbInspectionItemList(sbInspectionItem);
        return getDataTable(list);
    }

    /**
     * 获取巡检项详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(sbInspectionItemService.selectSbInspectionItemByItemId(itemId));
    }

    /**
     * 新增巡检项
     */
    @PostMapping
    public AjaxResult add(@RequestBody SbInspectionItem sbInspectionItem)
    {
        // 如果是临时配置ID（-1），使用默认值1
        if (sbInspectionItem.getConfigId() != null && sbInspectionItem.getConfigId() == -1) {
            // 使用默认配置ID
            sbInspectionItem.setConfigId(1L);
        }
        
        // 插入记录并获取生成的ID
        int rows = sbInspectionItemService.insertSbInspectionItem(sbInspectionItem);
        if (rows > 0) {
            // 返回包含ID的对象
            return AjaxResult.success(sbInspectionItem);
        }
        return AjaxResult.error("新增巡检项失败");
    }

    /**
     * 修改巡检项
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SbInspectionItem sbInspectionItem)
    {
        return toAjax(sbInspectionItemService.updateSbInspectionItem(sbInspectionItem));
    }

    /**
     * 删除巡检项
     */
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(sbInspectionItemService.deleteSbInspectionItemByItemIds(itemIds));
    }

    /**
     * 根据巡检配置ID查询巡检项列表
     */
    @GetMapping("/config/{configId}")
    public AjaxResult listByConfigId(@PathVariable("configId") Long configId)
    {
        System.out.println("根据巡检配置ID查询巡检项列表: " + configId);
        List<SbInspectionItem> list = sbInspectionItemService.selectSbInspectionItemByConfigId(configId);
        System.out.println("查询到巡检项数量: " + (list != null ? list.size() : 0));
        return success(list);
    }
} 